"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 数据库备节点多台虚机同时进行重建（build）操作；
Create At   : 2020/07/20
@zou_jialiang0505328114
Description :
    步骤 1.启动主备数据库，判断数据库状态；
    步骤 2.备数据库节点并行build；
Expect      :
History     :
    modified: 2021/01/22  modified by @wan005,使用库函数替换脚本中build操作
"""

import os
import unittest
import _thread
import time
from yat.test import macro
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.ComThread import ComThread
from testcase.utils.CommonSH import *


class Deletaduit(unittest.TestCase):
    commonshpri = CommonSH('PrimaryDbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')
    standby_1_sh = CommonSH('Standby1DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case161.py start------------')
        self.rootNode = Node()
        self.constant = Constant()
        self.sourcecmd = f'source {macro.DB_ENV_PATH}'
        self.startCmd = f'source {macro.DB_ENV_PATH};gs_om -t start'
        self.statusCmd = f'source {macro.DB_ENV_PATH};gs_om -t status --detail'
        self.dbUserNode1 = Node(node='PrimaryDbUser')
        self.dbUserNode = Node(node='PrimaryRoot')
        self.dbUserNode2 = Node(node='Standby1Root')
        self.dbUserNode3 = Node(node='Standby1DbUser')
        self.sh = CommonSH('Standby1DbUser')
        self.Constant = Constant()

    def test_startdb(self):
        buildcmd = f'{self.sourcecmd};gs_ctl build -D {macro.DB_INSTANCE_PATH}'
        # 多节点同时重建
        # _thread.start_new_thread(build, (self, 'build',))
        build_thread = ComThread(self.sh.executeGsctl, args=('build', self.Constant.REBUILD_SUCCESS_MSG,))
        build_thread.setDaemon(True)
        build_thread.start()

        time.sleep(2)

        result = self.standby_1_sh.buildStandby()
        self.log.info(result)
        self.assertIn('failed', result)

        time.sleep(5)

        build_thread.join(300)
        build_result = build_thread.get_result()
        self.assertTrue(build_result)

        time.sleep(10)

    def tearDown(self):
        self.dbUserNode1.sh(self.startCmd)
        time.sleep(10)
        status = self.dbUserNode1.sh(self.statusCmd).result()
        if not status:
            raise Exception("db status is not true, please check!")
        else:
            pass
        self.log.info(
            '------------------------------------reliability_dbsys161.py finish------------------------------------')
